package com.mgetool.ui.page;

import java.util.Stack;
import java.util.Vector;

import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.GC;

import com.mgetool.ui.DrawableInterface;


/**
 * 游戏各个界面抽象类，用于同一个surfaceView下不同界面、分层界面的显示与控制管理
 * 
 * @author wangbin
 *
 */
public abstract class PageView implements PageViewInterface{
	
	private Vector<DrawableInterface> childs;

	public PageView() {
		childs = new Vector<DrawableInterface>();
	}

	public void addChild(DrawableInterface moduleInterface) {
		if (moduleInterface != null) {
			childs.add(moduleInterface);
		} else {
			System.out.println(
					"failed: reason = moduleInterface is null !");
		}
	}

	/**
	 * 移除某个组件
	 * @param drawableInterface
	 */
	public void removeChild(DrawableInterface drawableInterface){
		drawableInterface.onDestroy();
		childs.removeElement(drawableInterface);
	}
	
	/**
	 * 移除某个组件
	 * @param drawableInterface
	 */
	public void removeAllChilds(){
		if(childs != null){
			for(int i = 0; i < childs.size();){
				DrawableInterface drawableInterface = childs.elementAt(i);
				drawableInterface.onDestroy();
				childs.removeElement(drawableInterface);
			}
		}
	}
	
	/**
	 * 将某个组件提至顶部
	 * @param drawable
	 */
	public void upChild2Top(DrawableInterface drawable){
		if(childs.contains(drawable)){
			childs.removeElement(drawable);
			childs.addElement(drawable);
		}
	}
	
	/**
	 * 将绘制消息分发给所有组件.
	 */
	protected void onDrawChilds(GC gc) {
		DrawableInterface moduleInterface = null;
		synchronized(childs){
			for (int i = 0; i < childs.size(); i++) {
				moduleInterface = childs.elementAt(i);
				moduleInterface.onDraw(gc);
				moduleInterface = null;
			}
		}
	}

	/**
	 * 将按键事件分发给所有组件.
	 */
	protected boolean onKeyChilds(int state, KeyEvent keyEvent) {
		boolean b = false;
		DrawableInterface moduleInterface = null;
		for (int i = 0; i < childs.size(); i++) {
			moduleInterface = childs.elementAt(i);
			b = moduleInterface.onKey(state, keyEvent);
			moduleInterface = null;
			if (b) {
				break;
			}
		}
		return b;
	}

	/**
	 * 将触屏消息分发给所有组件.
	 */
	protected boolean onTouchChilds(int state, MouseEvent event) {
		boolean b = false;
		DrawableInterface moduleInterface = null;
		for (int i = childs.size() - 1; i >= 0; i--) {
			moduleInterface = childs.elementAt(i);
			b = moduleInterface.onMouseEvent(state, event);
			moduleInterface = null;
			if (b) {
				break;
			}
		}
		return b;
	}

	/**
	 * 将更新消息分发给所有组件.
	 */
	protected void onUpdateChilds() {
		DrawableInterface moduleInterface = null;
		for (int i = 0; i < childs.size(); i++) {
			moduleInterface = childs.elementAt(i);
			moduleInterface.onUpdate();
			moduleInterface = null;
		}
//		ResourceManager.getInstance().releaseResource(hashCode());
	}

	@Override
	public void onDestroyed() {
		// TODO Auto-generated method stub
		if(childs != null){
			synchronized (childs) {
				for(int i = 0; i < childs.size(); i++){
					DrawableInterface drawableInterface = childs.elementAt(i);
					drawableInterface.onDestroy();
				}
				childs.removeAllElements();
			}
		}
//		ResourceManager.getInstance().releaseResource(hashCode());
	}
}
